package com.yuyang.wifi.views;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;

import com.yuyang.wifi.MyApplication;
import com.yuyang.wifi.constant.AppConstant;
import com.yuyang.wifi.utils.DeviceInfoUtils;
import com.yuyang.wifi.utils.SpUtil;
import com.yuyang.wifi.utils.ToastUtils;
import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTRewardVideoAd;

/**
 * created by mej
 * on 2020/12/2
 */
public class LoadCSJiliVideoUtils {

    static LoadCSJiliVideoUtils instance = new LoadCSJiliVideoUtils();
    private TTAdNative adNative;
    private String mCsjJiLiChannel;

    private boolean isVideoCompletePlay;

    public static LoadCSJiliVideoUtils getInstance() {
        return instance;
    }

    private Activity mContext;
    private boolean mIsLoaded1 = false; //视频是否加载完成
    private TTRewardVideoAd mttRewardVideoAd1;
    private TTAdNative mTTAdNative1;
    private OnAdCloseListener onAdCloseListener;
    private int adLoadCount;

    //一、加载穿山甲激励视频
    //BDAdvanceBaseAppNative 绑定激励视频回调
    public void load(final Activity context, String moduleName) {
        adLoadCount = 0;
        mContext = context;
        adNative = TTAdSdk.getAdManager().createAdNative(mContext);

//        String channelName = DeviceInfoUtils.getUmChannelName(mContext);
//        if (!TextUtils.isEmpty(channelName) && "sykq000001".equals(channelName)) {
//            //区分oppo 一、二、三、天,配置不同广告位ID
//            mCsjJiLiChannel = oppoAdChannelId;
//            String day = SpUtil.find(AppConstant.KEY_AD_DAY);
//            if (!TextUtils.isEmpty(day)) {
//                Integer integer = Integer.valueOf(day);
//                switch (integer) {
//                    case 1:
//                        mCsjJiLiChannel = MyApplication.getCsjJiLiChannel(AppConstant.KEY_CSJ_OPPO_DAY1);
//                        break;
//                    case 2:
//                        mCsjJiLiChannel = MyApplication.getCsjJiLiChannel(AppConstant.KEY_CSJ_OPPO_DAY2);
//                        break;
//                    default:
//                        mCsjJiLiChannel = MyApplication.getCsjJiLiChannel(AppConstant.KEY_CSJ_OPPO_DAY3);
//                        break;
//                }
//            } else {
//                mCsjJiLiChannel = MyApplication.getCsjJiLiChannel(AppConstant.KEY_CSJ_OPPO_DAY1);
//            }
//        } else if (!TextUtils.isEmpty(channelName) && "sykq000002".equals(channelName)) {
//            //区分oppo 一、二、三、天,配置不同广告位ID
//            mCsjJiLiChannel = oppoAdChannelId;
//            String day = SpUtil.find(AppConstant.KEY_AD_DAY);
//            if (!TextUtils.isEmpty(day)) {
//                Integer integer = Integer.valueOf(day);
//                switch (integer) {
//                    case 1:
//                        mCsjJiLiChannel = MyApplication.getCsjJiLiChannel(AppConstant.KEY_CSJ_VIVO_DAY1);
//                        break;
//                    case 2:
//                        mCsjJiLiChannel = MyApplication.getCsjJiLiChannel(AppConstant.KEY_CSJ_VIVO_DAY2);
//                        break;
//                    default:
//                        mCsjJiLiChannel = MyApplication.getCsjJiLiChannel(AppConstant.KEY_CSJ_VIVO_DAY3);
//                        break;
//                }
//            } else {
//                mCsjJiLiChannel = MyApplication.getCsjJiLiChannel(AppConstant.KEY_CSJ_VIVO_DAY1);
//            }
//        } else {
//            String otherChannelName = DeviceInfoUtils.getUmChannelName(mContext);
//            mCsjJiLiChannel = MyApplication.getCsjJiLiChannel(otherChannelName);
//        }

        mCsjJiLiChannel = MyApplication.getCsjAdIdChannel(moduleName);
        initCSJAd();
    }

    private void initCSJAd() {
        //模版渲染请求AdSlot
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(mCsjJiLiChannel)
                .setSupportDeepLink(true)
                .setAdCount(1)
                .setRewardName("coin")
                .setRewardAmount(1)
                .setExpressViewAcceptedSize(500, 500)//个性化模板广告需要设置期望个性化模板广告的大小,单位dp,激励视频场景，只要设置的值大于0即可。仅模板广告需要设置此参数
                .setImageAcceptedSize(1080, 1920)
                .setOrientation(TTAdConstant.VERTICAL)
                .setMediaExtra("media_extra")
                .setUserID(SpUtil.find(AppConstant.KEY_USER_ID))
                .build();
        //step5:请求广告
        //加载激励视频广告
        adNative.loadRewardVideoAd(adSlot, new TTAdNative.RewardVideoAdListener() {
            @Override
            public void onError(int i, String s) {
                Log.i("csj_ad", "csj_onError, adError========" + s);
                if (adLoadCount < 25) {
                    adLoadCount++;
                    initCSJAd();
                } else {
                    if (null != handlerCSJLooper) {
                        handlerCSJLooper.removeCallbacksAndMessages(null);
                    }
                }
            }

            @Override
            public void onRewardVideoAdLoad(TTRewardVideoAd ttRewardVideoAd) {
                if (ttRewardVideoAd == null) {

                } else {
                    bindRewardListener(ttRewardVideoAd);

                }
            }

            @Override
            public void onRewardVideoCached() {
                mIsLoaded1 = true;
            }

            @Override
            public void onRewardVideoCached(TTRewardVideoAd ttRewardVideoAd) {

            }


        });
    }

    //二、绑定穿山甲激励视频回调
    //BDAdvanceBaseAppNative 绑定激励视频回调
    TTRewardVideoAd mttRewardVideoAd;

    private void bindRewardListener(TTRewardVideoAd ttRewardVideoAd) {
        mttRewardVideoAd = ttRewardVideoAd;
        mttRewardVideoAd.setRewardAdInteractionListener(new TTRewardVideoAd.RewardAdInteractionListener() {
            @Override
            public void onAdShow() {
                isVideoCompletePlay = false;
            }

            @Override
            public void onAdVideoBarClick() {

            }

            @Override
            public void onAdClose() {
                initCSJAd();
                if (isVideoCompletePlay) {
                    if (null != onAdCloseListener) {
                        onAdCloseListener.onAdCloseListenerListener(true);
                    }
                } else {
                    if (null != onAdCloseListener) {
                        onAdCloseListener.onAdCloseListenerListener(false);
                    }
                    ToastUtils.showLongToast(mContext, "奖励领取失败，必须完整观看，1秒都不能少哦");
                }
            }

            @Override
            public void onVideoComplete() {
                isVideoCompletePlay = true;
                Log.e("csjjjjjj", "onVideoComplete: ===");
            }

            @Override
            public void onVideoError() {

            }

            @Override
            public void onRewardVerify(boolean b, int i, String s, int i1, String s1) {
                isVideoCompletePlay = true;
                Log.e("csjjjjjj", "onVideoComplete: ===" + s);
            }

            @Override
            public void onSkippedVideo() {

            }
        });

    }


    //三、播放穿山甲激励视频
    public void play(Activity activity) {
        if (mttRewardVideoAd != null && mIsLoaded1) {
            mttRewardVideoAd.showRewardVideoAd(activity);
            ToastUtils.showLongToast(mContext, "请勿提前关闭或跳过，必须完整观看，1秒都不能少哦");
//            ToastUtilsTime toastUtil3 = new ToastUtilsTime(mContext, R.layout.toast_center_horizontal, "看完视频可获得奖励");
//            toastUtil3.show(5000);
        } else {
            startCSJRoll();
            initCSJAd();
        }
    }

    private void startCSJRoll() {
        handlerCSJLooper.removeCallbacksAndMessages(null);
        handlerCSJLooper.sendEmptyMessageDelayed(0, 100);
    }

    Handler handlerCSJLooper = new Handler() {
        public void handleMessage(Message msg) {
            if (mIsLoaded1) {
                if (mttRewardVideoAd != null && mIsLoaded1) {
                    handlerCSJLooper.removeCallbacksAndMessages(null);
                    mttRewardVideoAd.showRewardVideoAd(mContext);
                    ToastUtils.showLongToast(mContext, "请勿提前关闭或跳过，必须完整观看，1秒都不能少哦");
//                    ToastUtilsTime toastUtil3 = new ToastUtilsTime(mContext, R.layout.toast_center_horizontal, "看完视频可获得奖励");
//                    toastUtil3.show(5000);
                }
            } else {
                handlerCSJLooper.sendEmptyMessageDelayed(0, 100);
//                Log.e("mej", "handleMessage:=== " + msg.obj);
//                if (adLoadCount < 5) {
//                    handlerCSJLooper.sendEmptyMessageDelayed(0, 200);
//                } else {
//                    handlerCSJLooper.removeCallbacksAndMessages(null);
//                }
            }
        }
    };

    public interface OnAdCloseListener {
        void onAdCloseListenerListener(boolean isPlayComplete);
    }

    public void setAdCloseListener(OnAdCloseListener onAdCloseListener) {
        this.onAdCloseListener = onAdCloseListener;
    }


}
